package com.sim.simserial.ui

import android.app.AlertDialog
import android.util.Log
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.navArgs
import com.drake.brv.utils.addModels
import com.drake.brv.utils.linear
import com.drake.brv.utils.models
import com.drake.brv.utils.setup
import com.drake.channel.receiveEvent
import com.infinit.selfhelp.common.base.ViewBindingFragment
import com.infinit.selfhelp.common.ext.onUI
import com.sim.serialport.library.seria.SerialManager
import com.sim.serialport.library.seria.ext.bytesToHex
import com.sim.serialport.library.seria.ext.formatString
import com.sim.serialport.library.seria.ext.hexStringToAscii
import com.sim.simserial.R
import com.sim.simserial.databinding.FragmentRequestAndResponseBinding
import com.sim.simserial.utils.SerialDataBuilder
import com.sim.simserial.viewmodel.SimViewModel
import com.sim.storage.enums.TransactionType
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject

@AndroidEntryPoint
class RequestAndResponseFragment : ViewBindingFragment<FragmentRequestAndResponseBinding>(R.layout.fragment_request_and_response) {
    private val args: RequestAndResponseFragmentArgs by navArgs()

    @Inject
    lateinit var serialManager: SerialManager

    private val simViewModel by viewModels<SimViewModel>()

    companion object {
        const val Request_terminal_status = "Request Terminal Status"
        const val LOGON = "LOGON"
        const val TMS = "TMS"
        const val GET_LAST_APPROVED_TRANSACTION = "Get Last Approved Transaction"
        const val Settlement = "Settlement"
        const val All_Nests_Purchase = "All NETS Purchase"
    }

    override fun initView() {
        super.initView()
        mBinding.tvLog.linear().setup {
            addType<String>(R.layout.item_log)
        }.models = mutableListOf<String>()

        when (args.typeString) {
            Request_terminal_status -> {
                val buildRequestTerminalStatus = SerialDataBuilder.buildRequestTerminalStatus()
                simViewModel.sendDataWithACK2(buildRequestTerminalStatus)
            }

            LOGON -> {
                val buildRequestTerminalStatus = SerialDataBuilder.buildLogonRequest()
                simViewModel.sendDataWithACK2(buildRequestTerminalStatus)
            }

            Settlement -> {
                SerialDataBuilder.buildNETSSettlement()
            }

            TMS -> {
                val buildRequestTerminalStatus = SerialDataBuilder.buildTMSRequest()
                simViewModel.sendDataWithACK2(buildRequestTerminalStatus)
            }
        }
    }

    override fun onEvent() {
    }

    override fun observe() {
        super.observe()
        receiveEvent<SimViewModel.LogEvent> {
            if (it.isPos) {
                mBinding.tvLog.addModels(listOf("pos: ${it.log}"))
            } else {
                mBinding.tvLog.addModels(listOf("terminal : ${it.log}"))
            }
        }
        receiveEvent<SimViewModel.ErrorReplyEvent> {
            val b = AlertDialog.Builder(requireContext())
            b.setTitle("Error")
            b.setMessage(it.error.errorTypeName)
            b.setPositiveButton("OK") { dialog, which -> }
            b.show()
        }
        receiveEvent<SimViewModel.WrongLRCEvent> {
            val b = AlertDialog.Builder(requireContext())
            b.setTitle("Error")
            b.setMessage(it.error)
            b.setPositiveButton("OK") { dialog, which -> }
            b.show()
        }

        receiveEvent<SimViewModel.ResponseEvent> {
            mBinding.tvLog.addModels(listOf("ACK-解析receiveData:\n ${it.response.toString()}"))
        }
    }
}
